package com.tianji.aigc.controller;


import cn.hutool.core.collection.CollStreamUtil;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.*;

import javax.print.Doc;
import java.nio.channels.Pipe;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/embedding")
@Slf4j
public class EmbeddingController {

    private final VectorStore vectorStore;
    private final EmbeddingModel embeddingModel;
    @PostMapping
    public void saveVectorStore(@RequestParam List<String>messages){
        List<Document> documentList = CollStreamUtil.
                toList(messages,message->Document.builder().text(message).build());

        vectorStore.add(documentList);

        log.info("保存向量成功,{}",documentList);
    }

    @GetMapping
    public EmbeddingResponse EmbeddingResponse(String message){
        return embeddingModel.embedForResponse(List.of( message));
    }

    @GetMapping("/search")
    public List<Document> search(String message){
        return vectorStore.similaritySearch(SearchRequest.builder().query( message).topK(5).build());
    }

    @GetMapping("/search/all")
    public List<Document> searchAll(String message){
        return vectorStore.similaritySearch(SearchRequest.builder().query("").topK(999).build());
    }

    @DeleteMapping
    public void deleteVectorStore(@RequestParam("ids") List<String> ids){
        vectorStore.delete(ids);
    }


}
